<template>
  <div class="mentors">
    <userSearchCondition @do-search="doSearch" placeholder="请输入创业导师名称"/>
    <div v-if="!isEmpty" class="mentors-list">
      <div
        @click="toDetail(item.id)"
        v-for="item in mentorsList"
        :key="item.id"
        class="mentors-list-item p-10 c-p"
      >
        <el-card>
          <template #header>
            <div class="card-header">
              <img style="height: 30px" v-if="item?.avatar" :src="item?.avatar" alt="头像" />
              <img v-else src="@/assets/images/default-avatar.png" alt="头像" />
              <span class="m-l-10">{{ item.name }}</span>
            </div>
          </template>
          <div class="school m-b-10">
            <span class="color-gray">所在学校：</span>
            <span>{{ item.school }}</span>
          </div>
          <div class="education m-b-10">
            <span class="color-gray">学历：</span>
            <span>{{ item.education }}</span>
          </div>
          <div class="field flex">
            <span class="color-gray f-s-0">研究领域：</span>
            <el-tag v-for="field in item.field" :key="field" class="m-r-10 m-b-10">{{ field }}</el-tag>
          </div>
          <div class="ability flex">
            <span class="color-gray f-s-0">专业能力：</span>
            <el-tag v-for="ability in item.ability" :key="ability" class="m-r-10 m-b-10">{{ ability }}</el-tag>
          </div>
        </el-card>
      </div>
    </div>
    <el-empty v-if="isEmpty" description="没有符合条件的数据" />
    <el-pagination
      v-if="!isEmpty"
      background
      class="m-t-10 m-b-10"
      style="justify-content: flex-end"
      :page-sizes="[3, 6, 12, 24, 48, 96]"
      layout="sizes, prev, pager, next"
      :total="total"
      :page-size="requestData.size"
      v-model:current-page="requestData.current"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup lang="ts">
import { getTalentPersonList } from '@/api/talentPerson';
import { onMounted, ref } from 'vue';
import { useRouter } from 'vue-router';
import userSearchCondition from '@/components/user-search-condition/index.vue';

const mentorsList = ref<any>([]);
const total = ref(0);
onMounted(() => {
  loadData();
});
const isEmpty = ref(false);
function loadData() {
  getTalentPersonList({
    ...requestData.value,
    category: 0 // 教师类别
  }).then((res: any) => {
    if (res.code === 200) {
      mentorsList.value = res.data.records;
      total.value = res.data.total;
      if (res.data.total === 0) {
        isEmpty.value = true;
      } else {
        isEmpty.value = false;
      }
    }
  });
}

function doSearch(query: any) {
  requestData.value.name = query?.nickName;
  requestData.value.field = query?.industrySector ? query.industrySector : undefined;
  loadData();
}

// 搜索的请求参数
const requestData = ref<{
  size: number;
  current: number;
  name?: string;
  field?: string[];
}>({
  size: 3,
  current: 1,
});

// 跳转到详情
const router = useRouter();
function toDetail(id: number) {
  router.push({
    name: 'mentorsDetail',
    query: {
      id: id,
    },
  });
}

// 分页
function handleSizeChange(val: number) {
  requestData.value.size = val;
  loadData();
}
function handleCurrentChange(val: number) {
  requestData.value.current = val;
  loadData();
}
</script>

<style lang="scss" scoped>
.mentors {
  padding: 20px;
  &-list {
    display: flex;
    flex-wrap: wrap;
    &-item {
      width: 33%;
      .card-header {
        display: flex;
        align-items: center;
      }
      .field, .ability {
        flex-wrap: wrap;
        .el-tag {
          margin-right: 10px;
          margin-bottom: 10px;
        }
      }
    }
  }
}
</style>